package com.bruce.pattern.structural.proxy.dynamicproxy;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.cglib.proxy.Enhancer;

/**
 * Created by bruce on 2018/11/8 23:12
 */
@Slf4j
public class MainTest {
    @Test
    public void cglibProxy() {
        long start = System.currentTimeMillis();
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(new CglibDBQueryInterceptor());
        //enhancer.setInterfaces(new Class[]{DBQueryI.class});
        enhancer.setSuperclass(DBQueryImpl.class);

        //DBQueryImpl dbQueryI = (DBQueryImpl) enhancer.create();
        DBQueryI dbQueryI = (DBQueryI) enhancer.create();
        log.info("cglib 动态代理创建时间" + (System.currentTimeMillis() - start));
        start = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            dbQueryI.userName("hahah");
        }
        log.info("调用耗时" + (System.currentTimeMillis() - start));

        Object singletonTarget = AopProxyUtils.getSingletonTarget(dbQueryI);



        //dbQueryI.helloCglib();
        //dbQueryI.helloCglib2();
    }


}
